<?php
/**
 * Created by PhpStorm.
 * User: lixiaohui
 * Date: 2015/9/21
 * Time: 21:08
 */

namespace Home\Controller;


use Think\Controller;
use Think\Log;

/**
 * Class ComplaintController
 * @package Home\Controller 投诉建议
 */
class ComplaintController extends Controller
{
    const DEFAULT_LOAD_MORE_COUNT = 10;

    //首页
    public function index()
    {
        $complaintModel = M('message_board', 'js_')->where("openId='%s' and status=1", getOpenId())->field('id,userContent,adminContent,userTime')->order('userTime desc,adminTime desc')->limit(self::DEFAULT_LOAD_MORE_COUNT)->select();
        $this->assign("complaintModel", $complaintModel);
        $this->assign("complaintCount", count($complaintModel));//数量
        $this->display();
    }

    //投诉详情
    public function detail()
    {
        $complaintModel = M('message_board', 'js_')->where("openId='%s' and id='%s' and status=1", getOpenId(), I('id'))->field('id,userContent,adminContent,userTime,adminTime,type')->find();
        $this->assign('complaintModel', $complaintModel);
        $this->display();
    }

    //添加投诉建议视图
    public function addView()
    {
        $this->display();
    }

    //添加投诉建议
    public function add()
    {
        Log::write('type=' . I("type") . ',content=' . I("content"));
        $data['id'] = getUUIDShort();
        $data['openid'] = getOpenId();
        $data['userContent'] = I('content');
        $data['status'] = 1;
        $data['userTime'] = array("exp", "now()");
        $data['type'] = I('type');
        //保存
        $saveResult = M('message_board', 'js_')->add($data);
        if ($saveResult > 0) {
            $this->ajaxReturn(array("status" => "success", "msg" => "提交成功!"));
        } else {
            $this->ajaxReturn(array("status" => "fail", "msg" => "提交失败!"));
        }
    }

    //加载更多
    public function more()
    {
        $complaintModel = M('message_board', 'js_')->where("openId='%s' and status=1", getOpenId())->field('id,userContent,adminContent,userTime')->order('userTime desc,adminTime desc')->limit(I('start'), self::DEFAULT_LOAD_MORE_COUNT)->select();
        $retArr = array(
            "status" => "success",
            "msg" => "加载更多成功!",
            "count" => count($complaintModel),
            "complaintModel" => $complaintModel
        );
        $this->ajaxReturn($retArr);
    }
}